<?php
class Admin
{
    public static function isLogged()
    {
        return isset($_SESSION[ADMIN_SESSION]);
    }

    public function  login($params)
    {
        $db = new Database();
        $params['pwd'] = encodePassword($params['pwd']);
        $result = $db->executeScalar('SELECT taikhoannv FROM nhanvien WHERE manhom=1 AND taikhoannv=:uid AND matkhaunv=:pwd', $params);
        $db->disconnect();

        if ($result == null) {
            return 0;
        } else {
            $_SESSION[ADMIN_SESSION] = $result;
            return 1;
        }
    }

    public static function getCurrentAdmin()
    {
        if (self::isLogged()) {
            return $_SESSION[ADMIN_SESSION];
        }
    }

    public function logout()
    {
        if (self::isLogged()) {
            unset($_SESSION[ADMIN_SESSION]);
            return 1;
        } else {
            return 0;
        }
    }
}
